<template>
  <div class="department" id="department">
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left">
          <el-radio-group
            v-model="radio"
            size="small"
            @change="modelType($event)"
          >
            <el-radio-button label="待办"></el-radio-button>
            <el-radio-button label="已办"></el-radio-button>
            <!-- #2b5a9d -->
          </el-radio-group>
        </div>
        <!-- 顶部按钮 -->
        <div class="print">
          <div class="printTable">
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="small"
              @click="yj_searchForm(getList)"
            ></el-button>
            <!-- 签发中,待签收,执行中,待复核,已结束,已打回   -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              v-if="getList.modelType == '3' && yj_edit == true"
              @click="edit('编辑')"
              >编辑</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              size="small"
              v-if="getList.modelType == '3' && yj_submit == true"
              @click="SubmitText(fId)"
              >提交</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-share"
              size="small"
              v-if="getList.modelType == '3' && yj_check == true"
              @click="edit('复核')"
              >复核</el-button
            >
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <div class="text_table">
        <tableData
          :height="navigatepageNums > 1 ? 'calc(100% - 50px)' : '100%'"
          :config="table_config"
          :dataList="tableData"
          :loading="loading"
          @selection-change="selectionChange"
          @sort-change="sortChange"
          @click-change="rowChange"
          @ClickOnLine="yj_viewForm"
          :page="getList.page"
          :pageSize="getList.pageSize"
        />
        <pagination
          v-if="navigatepageNums > 1"
          :info="queryCriteriaData"
          :treelength="treelength"
          :total="queryCriteriaData.total"
          :pageNum.sync="getList.page"
          :limit.sync="getList.pageSize"
          @pagination="yj_searchForm(getList)"
        />
      </div>
      <!--班组首件信息弹框-->
      <div class="Control-1 mechanism_dialogS closure">
        <el-dialog
          :title="dialogTitle"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisible"
          v-loading="loadingEditDialog"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div
            class="dialogBody common"
            :style="{ height: '650px' }"
            style="overflow: hidden"
          >
            <el-collapse v-model="activeNames" @change="changeCollapse">
              <el-collapse-item name="0">
                <template slot="title">
                  <div class="baseTitle">工作指令信息</div>
                </template>

                <div style="height: 285px; overflow-y: auto" class="publicForm">
                  <publicForm
                    ref="publicForm1"
                    lableWidth="120px"
                    :formObj="FromShow"
                    :formData="formData"
                    :timestamp="formData.fOrderfile"
                    :fileList="fSourcefileList"
                  >
                  </publicForm>
                </div>
              </el-collapse-item>
              <el-collapse-item name="1">
                <template slot="title">
                  <div class="baseTitle">指令签收</div>
                </template>
                <div style="height: 70px; overflow-y: auto" class="publicForm">
                  <publicForm
                    ref="publicForm1"
                    lableWidth="120px"
                    :formObj="putAway"
                    :formData="formData"
                  >
                  </publicForm>
                </div>
              </el-collapse-item>
              <el-collapse-item name="2">
                <template slot="title">
                  <div class="baseTitle">工作指令执行报告</div>
                </template>
                <div style="height: 165px; overflow-y: auto" class="publicForm">
                  <publicForm
                    ref="publicForm"
                    lableWidth="120px"
                    :formObj="execute"
                    :formData="formData"
                    :timestamp="formData.fReportfile"
                    :fileList="fSourcefileList1"
                    @selectChange="selectChange"
                    @inputChange="inputChange"
                    @delteFile="delteFile"
                    @inputFocus="inputFocus"
                  >
                  </publicForm>
                </div>
              </el-collapse-item>
              <el-collapse-item name="3" v-if="Sign == '复核'">
                <template slot="title">
                  <div class="baseTitle">复核意意见</div>
                </template>
                <div style="height: 70px; overflow-y: auto" class="publicForm">
                  <publicForm
                    ref="publicForm0"
                    lableWidth="120px"
                    :formObj="check"
                    :formData="formData"
                    @selectChange="selectChange"
                    @inputChange="inputChange"
                  >
                  </publicForm>
                </div>
              </el-collapse-item>
            </el-collapse>
          </div>
          <div class="newlyAdded_btn" v-if="btnShow">
            <el-button
              type="primary"
              @click="yj_saveForm(1)"
              v-if="Sign == '复核'"
              >复核保存</el-button
            >
            <el-button type="primary" @click="yj_saveForm(0)" v-else
              >保存</el-button
            >
            <el-button type="warning" @click="dialogEditVisible = false"
              >关闭</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getWorkOrderExecutionReportPageLists, //列表
  getWorkOrderExecutionReportByFid, //详情
  saveWorkOrderReport, //保存
  saveWorkOrderByRecheck, //复核
  commitWorkOrderReport, //提交
} from "@/api/request.js";
import TableData from "@/components/stationProductionPage/pitchManage/dyMonitorManage/el-tables"; //主页面列表
import Pagination from "@/components/public/el-pagination"; //主页面分页
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "../regexDetail.js"; //新增弹框配置项
// import tree from "@/components/public/el-trees"; //左侧机构树
export default {
  inject: ["reload"],
  name: "executeText",
  data() {
    return {
      name: "executeText",
      // 分页
      navigatepageNums: null, //是否显示分页
      queryCriteriaData: {},
      // 分页
      yj_check: null,
      yj_edit: null,
      yj_submit: null,

      radio: "待办",

      // 列表
      getList: {
        fCompanyid: "", // 左侧选中组织机构id
        page: 1, //页码
        pageSize: 20, //每页条数
        userId: "", // 查询待办或已办时传 列表页查询不传
        modelType: "", // 0代办 1已办 2表示列表页查询
        fQualitygridclass: "", // 上方查询条件质量网格
        fQualitygridtype: "", // 上方查询条件工程网格
      },
      fId: null, //当前行数据fId
      row: { fstatus: "" }, //选中行
      loading: false, //列表转载
      table_config: formJs.table_configText1,
      tableData: [], //表格列表数据列表
      // 列表

      // 弹框
      activeNames: ["0", "1", "2", "3"],
      Sign: "",
      timeYMD: null, //当前日期
      btnShow: true,
      dialogTitle: "",
      loadingEditDialog: false, //弹框loading
      dialogEditVisible: false, //新增弹框是否显示
      FromShow: formJs.FromShowLook, //信息查看配置参数
      putAway: formJs.putAwayLook,

      execute: null, //报告
      executeText: formJs.executeText, //报告---编辑
      executeLook: formJs.executeLook, //报告---查看

      check: null, //复核
      checkText: formJs.checkText, //复核---编辑
      checkLook: formJs.checkLook, //复核---查看

      formData: formJs.formData,
      //   CcpeoplesUserList: [], //复选人员框
      recordfiles: [], //删除文件
      fSourcefileList: [],
      fSourcefileList1: [],
      saveData: {},
      getUserInitialList: [], //指定签收人初始列表

      // 弹框

      userInfo: {}, //登录用户
    };
  },
  components: {
    TableData, //主页面列表
    Pagination, //分页
    publicForm, //表单
    // tree, //机构树
  },
  created() {},
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
    console.log("userInfo", this.userInfo);
    this.getList = {
      fCompanyid: "", // 左侧选中组织机构id
      userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
      page: 1, //页码
      pageSize: 20, //每页条数
      modelType: "3", // 0代办 1已办 不传或传空 表示列表页查询
      fQualitygridclass: "", // 上方查询条件质量网格
      fQualitygridtype: "", // 上方查询条件工程网格
    };
    this.getHeight();
    this.getData();
    this.yj_searchForm(this.getList);
    this.btnRole(this.name);
  },
  methods: {
    btnRole(name) {
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });
        this.yj_check = btnStr.includes("yj_check");
        this.yj_edit = btnStr.includes("yj_edit");
        this.yj_submit = btnStr.includes("yj_submit");
      } else {
        this.yj_check = true;
        this.yj_edit = true;
        this.yj_submit = true;
      }
    },
    modelType(e) {
      this.fId = "";
      this.row = "";
      if (e == "待办") {
        this.getList.modelType = "3";
        this.getList.userId = this.userInfo.userId;
      } else {
        this.getList.modelType = "4";
        this.getList.userId = this.userInfo.userId;
      }
      this.yj_searchForm(this.getList);
    },
    // 获取列表数据
    yj_searchForm(id) {
      this.row = { fstatus: "" }; //选中行
      this.loading = true;
      getWorkOrderExecutionReportPageLists(id)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.loading = false;
            that.tableData = res.data.pageInfo.list;
            that.queryCriteriaData = res.data.pageInfo;
            that.navigatepageNums =
              that.queryCriteriaData.navigatepageNums.length;
          } else {
            this.$message.error("列表", res.msg);
            this.loading = false;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    edit(title) {
      this.Sign = title;
      this.dialogTitle = title;
      this.recordfiles = [];
      this.fSourcefileList = [];
      this.fSourcefileList1 = [];
      this.btnShow = true;

      if (title == "编辑") {
        this.execute = this.executeText;
        if (this.fId == "" || this.fId == null) {
          this.$message.error("请先选择需要编辑的数据");
        } else {
          if (this.row.fstatus == "执行中") {
            this.loadingEditDialog = true; //弹框loading
            this.dialogEditVisible = true;
            this.getConcrete(this.fId);
          } else {
            this.$message.error("请选择信息状态为‘执行中’的数据");
          }
        }
      } else if (title == "复核") {
        this.execute = this.executeLook;
        this.check = this.checkText;
        if (this.fId == "" || this.fId == null) {
          this.$message.error("请先选择需要复核的数据");
        } else {
          if (this.row.fstatus == "待复核") {
            this.loadingEditDialog = true; //弹框loading
            this.dialogEditVisible = true;
            this.getConcrete(this.fId);
          } else {
            this.$message.error("请选择信息状态为‘待复核’的数据");
          }
        }
      }
    },
    // 获取详情
    getConcrete(id) {
      getWorkOrderExecutionReportByFid({ fId: id })
        .then((res) => {
          if (res.errcode == 0) {
            this.loadingEditDialog = false; //弹框loading
            this.formData = res.data.yjQmpMsWrokorderExecutionreport;
            if (this.Sign == "编辑") {
              if (
                this.formData.fReportreplydates == "" ||
                this.formData.fReportreplydates == null
              ) {
                this.formData.fReportreplydates = this.timeYMD; //执行回复日期
              }
              if (
                this.formData.fReportinformantname == "" ||
                this.formData.fReportinformantname == null ||
                this.formData.fReportinformantid == "" ||
                this.formData.fReportinformantid == null
              ) {
                this.formData.fReportinformantname = this.userInfo.realName; //填报人
                this.formData.fReportinformantid = this.userInfo.userId;
              }
              // if (
              //   this.formData.fReportfile == "" ||
              //   this.formData.fReportfile == null
              // ) {
              //   this.formData.fReportfile =
              //     "fReportfile" + new Date().valueOf();
              // }
            } else if (this.Sign == "复核") {
              if (
                this.formData.fRecheckname == "" ||
                this.formData.fRecheckname == null ||
                this.formData.fRecheckid == "" ||
                this.formData.fRecheckid == null
              ) {
                this.formData.fRecheckname = this.userInfo.realName; //复核人
                this.formData.fRecheckid = this.userInfo.userId;
              }
              if (
                this.formData.fRecheckdates == "" ||
                this.formData.fRecheckdates == null
              ) {
                this.formData.fRecheckdates = this.timeYMD; //复核时间
              }
              this.formData.fRecheckresult = "复核通过。";
            }
            this.fSourcefileList = res.data.fOrderfilelist; //附件资料
            this.fSourcefileList1 = res.data.fReportfilelist; //执行报告附件材料
          } else {
            this.loadingEditDialog = false; //弹框loading
            return this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 保存
    yj_saveForm(num) {
      if (num == 0) {
        // 保存
        if (this.$refs.publicForm.submitForm()) {
          this.saveData = {
            fId: this.formData.fId, //保存与修改都需要传入fid
            fReportnumber: this.formData.fReportnumber, //执行报告编号
            fReportreplydates: this.formData.fReportreplydates, //执行回复日期
            fInsprctionprojectname: this.formData.fInsprctionprojectname, //工程名称
            fInspectionproject: this.formData.fInspectionproject, //工程项目及现场位置
            fReportmessage: this.formData.fReportmessage, //执行报告内容
            fReportinformantname: this.userInfo.realName, //填报人姓名
            fReportinformantid: this.userInfo.userId, //填报人id
            fReportfile: this.formData.fReportfile, //执行报告附件材料
            recordfiles: this.recordfiles, // 删除文件id集合
          };
          saveWorkOrderReport(this.saveData)
            .then((res) => {
              if (res.errcode == 0) {
                this.yj_searchForm(this.getList);
                this.dialogEditVisible = false;
                this.$message.success("新增成功");
              } else {
                this.$message.error(res.msg);
                console.log("接口报错");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          console.log("error");
        }
      } else if (num == 1) {
        if (this.$refs.publicForm0.submitForm()) {
          var saveData1 = {
            fId: this.formData.fId, //传此表示修改
            fRecheckresult: this.formData.fRecheckresult, //签发单位意见：复核通过。/复核不通过。(注意要有句号)
            fRecheckmessage: this.formData.fRecheckmessage, //复核情况说明
            fRecheckname: this.userInfo.realName, //复核人姓名
            fRecheckid: this.userInfo.userId, //复核人id
            fRecheckdates: this.formData.fRecheckdates, //复核时间
          };
          saveWorkOrderByRecheck(saveData1)
            .then((res) => {
              if (res.errcode == 0) {
                this.yj_searchForm(this.getList);
                this.dialogEditVisible = false;
                this.$message.success("保存成功");
              } else {
                this.$message.error(res.msg);
                console.log("复核接口报错");
              }
            })
            .catch((error) => {
              console.log(error, "复核接口报错");
            });
        } else {
          console.log("error");
        }
      }
    },

    //提交
    SubmitText(id) {
      if (id != "" && id != null) {
        if (this.row.fstatus == "执行中") {
          if (this.row.frepotnumber != "" && this.row.frepotnumber != null) {
            this.$confirm("是否继续提交?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                this.loading = true;
                commitWorkOrderReport({
                  fId: id,
                  userId: this.userInfo.userId,
                  userName: this.userInfo.realName,
                })
                  .then((res) => {
                    if (res.errcode == 0) {
                      this.loading = false;
                      this.yj_searchForm(this.getList);
                      this.$message.success("提交成功");
                    } else {
                      this.$message.error("提交失败", res.msg);
                      this.loading = false;
                    }
                  })
                  .catch(() => {
                    console.log("提交失败");
                    this.loading = false;
                  });
              })
              .catch(() => {
                this.$message("取消提交");
              });
          } else {
            return this.$message.error("请先编辑，再提交");
          }
        } else {
          return this.$message.error("请选择信息状态为‘执行中’的数据");
        }
      } else {
        return this.$message.error("请先选择需要提交的数据");
      }
    },

    // 获取页面容器高度
    getHeight() {
      var h = document.documentElement.clientHeight; //获取页面可见高度
      this.dialogHeight = h * 0.36 + "px";
    },

    // 点击当前行变色
    rowChangeColor(val) {
      return val;
    },
    // 点击查看
    yj_viewForm(row, label) {
      this.row = row;
      this.fId = row.fId;
      if (label == "指令单编号") {
        this.editText(this.fId);
      }
    },
    // 查看
    editText(id) {
      this.execute = this.executeLook;
      this.check = this.checkLook;
      this.dialogTitle = "查看";
      this.Sign = "查看";
      this.btnShow = false;
      if (id != "" && id != null) {
        this.loadingEditDialog = true; //弹框loading
        this.dialogEditVisible = true;
        getWorkOrderExecutionReportByFid({ fId: id })
          .then((res) => {
            if (res.errcode == 0) {
              var that = this;
              that.loadingEditDialog = false;
              this.formData = res.data.yjQmpMsWrokorderExecutionreport;
              this.fSourcefileList = res.data.fOrderfilelist; //附件资料
              this.fSourcefileList1 = res.data.fReportfilelist; //执行报告附件材料
            } else {
              this.$message.error("查看", res.msg);
              this.loadingEditDialog = false;
            }
          })
          .catch(() => {
            console.log("查看---详情接口报错");
            this.loadingEditDialog = false;
          });
      } else {
        return this.$message.error("请先选择需要查看的数据");
      }
    },
    getData() {
      var time = new Date();
      const y = time.getFullYear();
      const m = (time.getMonth() + 1 + "").padStart(2, "0");
      const d = (time.getDate() + "").padStart(2, "0");
      this.timeYMD = `${y}-${m}-${d}`;
    },
    changeCollapse(e) {
      return e;
    },
    // 点击当前
    rowChange(val) {
      this.row = val;
      this.fId = val.fId;
    },
    // 刪除
    delteFile(file) {
      for (let index = 0; index < this.fSourcefileList1.length; index++) {
        if (file.uid == this.fSourcefileList1[index].uid) {
          if (this.fSourcefileList1[index].atta == undefined) {
            this.recordfiles.push({
              fId: this.fSourcefileList1[index].fId,
            });
          } else {
            this.recordfiles.push({
              fId: this.fSourcefileList1[index].atta.fId,
            });
          }
          this.fSourcefileList1.splice(index, 1);
        }
      }
    },
    inputChange(value, index, prop) {
      console.log("inputChange", value, index, prop);
    },
    inputFocus(index, prop) {
      if (prop == "fOrderreceiptname") {
        this.getTree();
      }
    },
    selectChange(value, index, prop) {
      console.log("下拉框选择", value, index, prop);
    },
    // 多选方法
    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};
</script>

<style scoped>
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;
  height: 30px;
  width: 100%;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}
/* 弹框 */
.publicForm {
  display: flex;
}
.publicForm .mouldForm /deep/ .el-form-item__content {
  margin-left: 130px !important;
}
.publicFormBox {
  width: 50%;
  height: 100%;
}
.closure /deep/ .el-input__suffix {
  left: 91%;
}
/* 弹框 */
</style>